package com.lsfb.light.Utils;

import android.content.Context;
import android.graphics.Rect;
import android.view.TouchDelegate;
import android.view.View;

import com.lsfb.lsefbbaseviews.utils.DensityUtils;
import com.zgscwjm.lsfbbasetemplate.utils.LsfbLog;

/**
 * Created by nonsense on 2017/12/20.
 */

public class ViewUtils {

    public static void setTouchDelegateMore(final View view, Context context) {
        if (view != null && view.getParent() != null) {
            setTouchDelegate(view, 10, context);
        } else {
            LsfbLog.e("出现了异常，view为空或者parent为空");
        }
    }


    public static void setTouchDelegate(final View view, final int expandTouchWidth, Context context) {
        final View parentView = (View) view.getParent();
        final int expandTouchWidthdp = DensityUtils.dp2px(context, expandTouchWidth);
        parentView.post(new Runnable() {
            @Override
            public void run() {
                final Rect rect = new Rect();
                view.getHitRect(rect); // view构建完成后才能获取，所以放在post中执行
                LsfbLog.e("子view的高度"+rect.top);
                // 4个方向增加矩形区域
                rect.top -= expandTouchWidthdp;
                rect.bottom += expandTouchWidthdp;
                rect.left -= expandTouchWidthdp;
                rect.right += expandTouchWidthdp;
                parentView.setTouchDelegate(new TouchDelegate(rect, view));

//                if (parentView.getParent() instanceof View)
//                {
//                    ((View)parentView.getParent().getParent().getParent().getParent()).setTouchDelegate(new TouchDelegate(rect,view));
//                    ((View) parentView.getParent()).setTouchDelegate(new TouchDelegate(rect, view));
//                }
            }
        });
    }
}
